package org.open.sharedRadar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Login extends Activity{
	
	private EditText user;
    private EditText pass;
    DBAdapter myDBAdapter = new DBAdapter(this);
	public DBAdapter getMyDBAdapter() {
		return myDBAdapter;
	}
	public void setMyDBAdapter(DBAdapter myDBAdapter) {
		this.myDBAdapter = myDBAdapter;
	}
	
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);		
		this.setMyDBAdapter(myDBAdapter);
	    CurentUserProperties.getInstance().myDBAdapter = myDBAdapter;
	    
	    user =  (EditText) findViewById(R.id.userTxt);                             
	    pass =  (EditText) findViewById(R.id.passTxt);
	    
	    myDBAdapter.open();
	    myDBAdapter.parseUserDataToFromDB(false);
	    myDBAdapter.close();
	    String parola = CurentUserProperties.getInstance().currentPass;
	    user.setText(CurentUserProperties.getInstance().currentUserId);
	    pass.setText(parola);
	     
	    Button buttonOK = (Button) findViewById(R.id.loginBtn);
	    buttonOK.setOnClickListener(new OnClickListener(){
		   public void onClick(View v) {
			   if(user.getText().length() < 1 || pass.getText().length() <1)
				{
					Toast.makeText(getBaseContext(), "Trebuie sa intorduceti un nume de utilizator si o parola", Toast.LENGTH_SHORT).show();            	   
				}
				else
				{
				    //SoapObject
					String userSer = user.getText().toString();
					String passSer=  pass.getText().toString();
					 try
					   {
					       //androidHttpTransport.call(SOAP_ACTION, envelope);
					       //resultsRequestSOAP =  envelope.getResponse();
					       //String  result = resultsRequestSOAP.toString();
						   WebServiceHandler handler = new WebServiceHandler();
						   String result = handler.performLogin(userSer, passSer);
					       if(result.toUpperCase().equals("SUCCESS"))
						   {					    
					    	   CurentUserProperties.getInstance().currentUserId = userSer;	
					    	   CurentUserProperties.getInstance().currentPass = passSer;
					    	   myDBAdapter.open();
					    	   myDBAdapter.synchronizeFriendsReqWithWeb();
					    	   myDBAdapter.close();
					    	   Integer radius = CurentUserProperties.getInstance().myRadius;
					    	   if(radius == null)
					    		   CurentUserProperties.getInstance().myRadius = 5000; 
					    	   Intent idAdd = new Intent(Login.this,Menu.class);
					    	   startActivity(idAdd);              	   
						   }
						   else
						   {
							   Toast.makeText(getBaseContext(), "Autentificare esuata! " + result, Toast.LENGTH_SHORT).show();            	   
						   }
					   }
					   catch (Exception aE)
					   {
					       aE.printStackTrace ();;
					   }
				}
		   }
	    });
	    
	    Button buttonClose = (Button) findViewById(R.id.cancelBtn);
	    buttonClose.setOnClickListener(new OnClickListener(){
	    	public void onClick(View v) {			
	 		   try
	 		   {
	 			   	Intent idAdd = new Intent(Login.this,SharedRadar.class);
	 			  	startActivity(idAdd);
	 		   }
	 		   catch (Exception aE)
	 		   {
	 		       aE.printStackTrace ();;
	 		   }
	 			
	 		}
	    });    
	   
		
		
	}
}
